﻿using DeviceCommons.DataHandling;
using DeviceCommons.DeviceMessages.Abstractions;
using DeviceCommons.DeviceMessages.Models.V1;
using System.Buffers;

namespace DeviceCommons.DeviceMessages.Serialization.V2.Serializers
{
    public class DeviceMessageInfoReadingsSerializer : AbstractMessageSerializer<IDeviceMessageInfoReadings>, IDeviceMessageInfoReadingsSerializer
    {
        public DeviceMessageInfoReadingsSerializer() : base(new DeviceMessageInfoReadings())
        {
        }

        public override void Serializer(ArrayBufferWriter<byte> writer, IDeviceMessageInfoReadings message)
        {
            ArgumentNullException.ThrowIfNull(message);
            ArgumentNullException.ThrowIfNull(message.ReadingArray);
            writer.Write([message.Count]);
            if (message.Count > 0)
            {
                foreach (var reading in message.ReadingArray)
                {
                    DeviceMessageSerializerProvider.InfoReadingV2Ser.Serializer(writer, reading);
                }
            }
        }
    }
}
